﻿using NPOI.SS.Format;
using System;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ExcelOperateForNPOI
{
    /// <summary>
    /// 公共操作类
    /// </summary>
    public static class C_Util
    {
        /// <summary>
        /// 获取单元格格式化代码的类型
        /// </summary>
        /// <param name="FormatStr">格式化代码</param>
        /// <returns></returns>
        public static CellFormatType GetCellFormatType(string FormatStr)
        {
            if (FormatStr == null)
                return CellFormatType.GENERAL;

            Match iMatchObj = CellFormatPart.FORMAT_PAT.Match(FormatStr);
            if (!iMatchObj.Success)
            {
                throw new Exception("检查格式化代码类型识别，" + FormatStr);
            }

            string iGroupStr = iMatchObj.Groups[CellFormatPart.SPECIFICATION_GROUP].Value;

            #region NPOI的源代码
            string fdesc = iGroupStr.Trim();
            if (fdesc.Equals("") || fdesc.Equals("General", StringComparison.InvariantCultureIgnoreCase))
            {
                return CellFormatType.GENERAL;
            }
            MatchCollection matchs = CellFormatPart.SPECIFICATION_PAT.Matches(fdesc);
            bool flag = false;
            bool flag2 = false;
            foreach (Match match in matchs)
            {
                string str = match.Groups[0].Value;
                if (str.Length <= 0)
                {
                    continue;
                }
                char ch = str[0];
                if (ch <= 'M')
                {
                    switch (ch)
                    {
                        case '?':
                        case '#':
                            return CellFormatType.NUMBER;

                        case '@':
                            return CellFormatType.TEXT;

                        case '0':
                            goto Label_0128;

                        case 'D':
                            goto Label_011B;

                        case 'H':
                        case 'M':
                            goto Label_0124;
                    }
                    continue;
                }
                if (ch <= 'd')
                {
                    switch (ch)
                    {
                        case 'Y':
                        case 'd':
                            goto Label_011B;

                        case '[':
                            return CellFormatType.ELAPSED;

                        case 'S':
                            goto Label_0124;
                    }
                    continue;
                }
                if (ch <= 'm')
                {
                    switch (ch)
                    {
                        case 'h':
                        case 'm':
                            goto Label_0124;
                    }
                    continue;
                }
                if (ch == 's')
                {
                    goto Label_0124;
                }
                if (ch != 'y')
                {
                    continue;
                }
                Label_011B:
                return CellFormatType.DATE;
                Label_0124:
                flag = true;
                continue;
                Label_0128:
                flag2 = true;
            }
            if (flag)
            {
                return CellFormatType.DATE;
            }
            if (flag2)
            {
                return CellFormatType.NUMBER;
            }
            return CellFormatType.TEXT;

            #endregion
        }

        /// <summary>
        /// 获取对象指定属性名称的属性值
        /// </summary>
        /// <param name="DataObj">对象</param>
        /// <param name="PropertyName">属性名</param>
        /// <returns></returns>
        public static string GetPropertyValue(object DataObj, string PropertyName)
        {
            Type iObjType = DataObj.GetType();
            PropertyInfo iPropertyObj = iObjType.GetProperty(PropertyName);
            if (iPropertyObj == null) return "";
            object iPropertValueObj = iPropertyObj.GetValue(DataObj, null);
            if (iPropertValueObj == null) return "";

            return iPropertValueObj.ToString();
        }
    }
}